package cn.bj.xd.mdy.util;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.Properties;


/**
 * 配置文件读取工具类
 *
 * @author <a href="mailto:xd502djj@163.com">dongjunjie</a>
 * @date 2025-08-09 11:58
 **/
@Slf4j
public class PropertyUtil {
    private static Properties prop = new Properties();

    static {
        try (InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream("mingdao.properties")) {
            if (in == null) {
                log.error("配置文件未找到！");
            } else {
                prop.load(in);
                log.info("配置文件加载成功！");
            }
        } catch (Exception e) {
            log.error("配置文件加载失败！", e);
        }
    }

    public static String getProperty(String key) {
        if (prop == null) {
            return null;
        }
        return prop.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        String value = getProperty(key);
        if (value == null) {
            return defaultValue;
        }
        return value;
    }

    public static Integer getProperty(String key, Integer defaultValue) {
        String value = getProperty(key);
        if (value == null) {
            return defaultValue;
        }
        return Integer.parseInt(value);
    }

    public static Long getProperty(String key, Long defaultValue) {
        String value = getProperty(key);
        if (value == null) {
            return defaultValue;
        }
        return Long.parseLong(value);
    }

    public static Boolean getProperty(String key, Boolean defaultValue) {
        String value = getProperty(key);
        if (value == null) {
            return defaultValue;
        }
        return Boolean.parseBoolean(value);
    }
}
